package com.ruoyi.eqmonitor.controller;

import java.text.SimpleDateFormat;
import java.util.List;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.eqmonitor.dispose.service.IExteriorSiteAlarmService;
import com.ruoyi.eqmonitor.domain.OptHandleFlowRecord;
import com.ruoyi.eqmonitor.domain.OptPatrolEvent;
import com.ruoyi.eqmonitor.service.*;
import com.ruoyi.eqmonitor.smdtv.service.ISmdtvOutletProblemService;
import com.ruoyi.eqmonitor.smdtv.service.ISmdtvOverflowWellService;
import com.ruoyi.system.api.model.LoginUser;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.eqmonitor.domain.OptPatrolTask;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import java.util.Arrays;
import java.util.Date;
import java.util.Set;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.security.utils.SecurityUtils;

import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 巡检任务
 * @author ruoyi
 * @date 2022-12-05
 */
@Slf4j
@RestController
@RequestMapping("/patrolTask")
public class OptPatrolTaskController extends BaseController{

    @Autowired
    private IOptPatrolTaskService optPatrolTaskService;

    /**
     * 分页查询任务列表 用户权限
     */
    @ApiOperation("分页查询任务列表 用户权限")
    @GetMapping("/list")
    public TableDataInfo<?> list(OptPatrolTask optPatrolTask){
        startPage();
        LoginUser user = SecurityUtils.getLoginUser();
        if (user == null) return getDataTable(ListUtil.empty());
        Set<String> roles= user.getRoles();
        if(!roles.contains("admin")) {
            //获取当前登录人员,赋值任务接收人为当前登录人员
            optPatrolTask.setReceiveUserId(user.getUserid());
        }
        return getDataTable(optPatrolTaskService.selectOptPatrolTaskList(optPatrolTask));
    }

    /**
     * 分页查询任务列表
     */
    @ApiOperation("分页查询任务列表")
    @GetMapping("/listData")
    public TableDataInfo<?> listData(OptPatrolTask optPatrolTask){
        startPage();
        return getDataTable(optPatrolTaskService.selectOptPatrolTaskList(optPatrolTask));
    }

    /**
     * 根据id查询任务详情
     */
    @ApiOperation("根据id查询任务详情")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id){
        return AjaxResult.success(optPatrolTaskService.selectOptPatrolTaskById(id));
    }

    /**
     * 新增任务
     */
    @ApiOperation("新增任务")
    @Log(title = "巡检任务", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OptPatrolTask optPatrolTask){
       return toAjax(optPatrolTaskService.save(optPatrolTask));
    }

    /**
     * 修改任务
     */
    @ApiOperation("修改任务")
    @PutMapping
    public AjaxResult edit(@RequestBody OptPatrolTask optPatrolTask){
    	optPatrolTask.setUpdateTime(new Date());
    	optPatrolTask.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(optPatrolTaskService.updateById(optPatrolTask));
    }

    /**
     * 任务确认
     */
    @ApiOperation("任务确认")
    @PutMapping("/confirm")
    public AjaxResult confirm(@RequestBody OptPatrolTask optPatrolTask){
        return toAjax(optPatrolTaskService.confirm(optPatrolTask));
    }

    /**
     * 任务完成(清除)
     */
    @ApiOperation("任务完成(清除)")
    @PutMapping("/finish")
    public AjaxResult finish(@RequestBody OptPatrolTask optPatrolTask){
        return toAjax(optPatrolTaskService.finish(optPatrolTask));
    }

    /**
     * 删除任务
     */
    @ApiOperation("删除任务")
    @Log(title = "巡检任务", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids){
        return toAjax(optPatrolTaskService.removeByIds(Arrays.asList(ids)));
    }

    /**
     * 导出巡检任务列表
     */
    @ApiOperation("导出巡检任务列表")
    @Log(title = "巡检任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OptPatrolTask optPatrolTask){
        List<OptPatrolTask> list = optPatrolTaskService.selectOptPatrolTaskList(optPatrolTask);
        ExcelUtil<OptPatrolTask> util = new ExcelUtil<OptPatrolTask>(OptPatrolTask.class);
        util.exportExcel(response, list, "巡检任务数据");
    }

    /**
     * 自动生成设备报警任务并下派任务
     */
    @ApiOperation("自动生成设备报警任务并下派任务")
    @Log(title = "自动生成设备报警任务并下派任务", businessType = BusinessType.INSERT)
    @PostMapping("/deviceTaskAdd")
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult deviceTaskAdd(@RequestBody OptPatrolTask optPatrolTask){
       return toAjax(optPatrolTaskService.deviceTaskAdd(optPatrolTask));
    }

}
